/**
 * 
 */
package com.icbc.common.fileupload;

import java.util.List;

import com.icbc.cte.base.TranFailException;
import com.icbc.ctp.core.IContext;
import com.icbc.ctp.core.annotations.Input;
import com.icbc.ctp.core.annotations.Output;
import com.icbc.ctp.core.impl.OperationStep;

/**
 * TODO 类的描述：。 
 * <pre>
 *
 * </pre>
 *
 * <pre>
 * modify by lenovo on 2016-3-23
 *    fix->1.
 *         2.
 * </pre> 
 */
@Input(name = {"fileNameList","Filename"}, type = {"java:java.util.List","java:java.lang.String"}, mustInput = {true,true}, isVariable = {true,true}, isArray = {false,false}, description = {"上传文件全名","上传文件"})
@Output(name = { "relFileName","oriFileName","fileUrl", "retCode" }, type = { "java:java.lang.String","java:java.lang.String",
		"java:java.lang.String",
		"java:java.lang.String" }, isArray = { false,false,false, false }, description = {
		"上传文件全名","原名字","上传文件路径", "获取文件名结果" })
public class GetFileName extends OperationStep {

	/* (non-Javadoc)
	 * @see com.icbc.ctp.core.impl.OperationStep#execute(com.icbc.ctp.core.IContext)
	 */
	@Override
	protected int execute(IContext context) throws TranFailException {
		// TODO Auto-generated method stub
		try{
			List fileNameList = (List) getInputValue(context,"fileNameList");
			String fileNameOld = (String)getInputValue(context,"Filename");
			
			String fileUrl =  (String)fileNameList.get(0);
			int index = fileUrl.lastIndexOf("/");
			
			if(index ==-1){
				index = fileUrl.lastIndexOf("\\");
			}			
			String fileName = fileUrl.substring(index+1,fileUrl.length());
			index = fileName.lastIndexOf(".");
			String fileExt = fileName.substring(index+1,fileName.length());
			String fileNameWithOutExt = fileName.substring(0,index);
			fileName = fileNameWithOutExt.substring(fileNameWithOutExt.lastIndexOf(".")+1,fileNameWithOutExt.length());			
			
			
			
			
			context.setValueAt("fileUrl", fileUrl);
			context.setValueAt("oriFileName", fileName + "." + fileExt);
			context.setValueAt("relFileName", fileNameWithOutExt + "." + fileExt);
			context.setValueAt("retCode", 0);
			
		}catch(Exception e){
			System.out.println(e);
			context.setValueAt("retCode", -1);
		}
		return 0;
	}

}
